home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / arexx-scripts / arexx-pack / rexxarplib.lha / RxArpLib / rexx / Rexxcalc.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1992-02-26  |  3.4 KB  |  146 lines

  1. /*
  2. **  This implements a simple calculator in REXX with intuition interface.
  3. **  Intended to serve as an example for RexxArpLib 2.0 and later, NOT as
  4. **  an example of how to write a calculator...
  5. **
  6. **  By W.G.J. Langeveld, november 1988.
  7. */
  8.  
  9. /*
  10. *   Set up a host
  11. */
  12. if ~exists('t:CreateHost') then 
  13.   address command "echo >t:CreateHost.rexx /* */ call createhost('"'REXXCALCHOST'"','"'REXXCALCPORT'"')"
  14.  
  15. /* CreateHost muß als Hintergrundprozeß gestartet werden */
  16. address command "run rx t:CreateHost.rexx"
  17.  
  18. /*
  19. *   Wait until it is ready. It times out after a while, so do a few of them.
  20. *   May not be necessary, try it on your system for best results.
  21. */
  22. WaitForPort REXXCALCHOST
  23. WaitForPort REXXCALCHOST
  24. WaitForPort REXXCALCHOST
  25.  
  26. /*
  27. *   Open the window
  28. */
  29. idcmp = 'CLOSEWINDOW+GADGETUP'
  30. flags = 'WINDOWCLOSE+WINDOWDRAG+WINDOWDEPTH+BACKFILL'
  31. call OpenWindow(REXXCALCHOST, 50, 50, 180, 130, idcmp, flags)
  32.  
  33. /*
  34. *   Add all the gadgets
  35. */
  36. do i = 7 to 9
  37.  call AddGadget(REXXCALCHOST, 20+(i-7)*32, 50, i, i, i)
  38. end
  39.  
  40. do i = 4 to 6
  41.  call AddGadget(REXXCALCHOST, 20+(i-4)*32, 70, i, i, i)
  42. end
  43. do i = 1 to 3
  44.  call AddGadget(REXXCALCHOST, 20+(i-1)*32, 90, i, i, i)
  45. end
  46.  
  47. call AddGadget(REXXCALCHOST,  20, 110, 0,   "  0  ", 0)
  48. call AddGadget(REXXCALCHOST,  84, 110, ".", ".", ".")
  49. call AddGadget(REXXCALCHOST, 120,  70, "X", "X", "*")
  50. call AddGadget(REXXCALCHOST, 152,  70, "/", "/", "/")
  51. call AddGadget(REXXCALCHOST, 120,  90, "+", "+", "+")
  52. call AddGadget(REXXCALCHOST, 152,  90, "-", "-", "-")
  53. call AddGadget(REXXCALCHOST, 120,  50, "C", " CLR ", "C")
  54. call AddGadget(REXXCALCHOST, 120, 110, "=", "  =  ", "=")
  55.  
  56. /*
  57. *   Draw the dialogue area
  58. */
  59. call SetDrMd(REXXCALCHOST, JAM1)
  60. call SetAPen(REXXCALCHOST, 2)
  61. call RectFill(REXXCALCHOST, 10, 20, 170, 40)
  62. call SetAPen(REXXCALCHOST, 1)
  63. call RectFill(REXXCALCHOST, 15, 25, 165, 35)
  64. call SetReqColor(REXXCALCHOST, PROMPTPEN, 3)
  65. call SetReqColor(REXXCALCHOST, BACKPEN, 1)
  66.  
  67. /*
  68. *   Initialize some things first. Make sure we trap errors.
  69. */
  70. signal on syntax
  71.  
  72. quitflag = 0
  73. register = 0
  74. number = 0
  75. newnum = 1
  76.  
  77. call WindowText(REXXCALCHOST, "\  "||number)
  78.  
  79. /*
  80. *   This is the part that waits for messages.
  81. *   Study carefully: you have to be sure to get ALL messages.
  82. */
  83. mp = openport(REXXCALCPORT)
  84.  
  85. start:
  86.  
  87. do forever
  88.    if quitflag = 1 then leave
  89.    t = waitpkt(REXXCALCPORT)
  90.    do forever
  91.       p = getpkt(REXXCALCPORT)
  92.       if c2d(p) = 0 then leave
  93.  
  94.       arg = getarg(p)
  95.       t = reply(p, 0)
  96.  
  97. /*
  98. *   Phew! Got a message. Handle it. All this is trivial...
  99. */
  100.       if arg = CLOSEWINDOW then do
  101.          call CloseWindow(REXXCALCHOST)
  102.          quitflag = 1
  103.       end
  104.       else if index("1234567890.", arg) ~= 0 then do
  105.          if newnum = 1 then do
  106.             number = arg
  107.             newnum = 0
  108.          end
  109.          else number = number||arg
  110.       end
  111.       else if arg = "C" then do
  112.          register = 0
  113.          number   = 0
  114.          newnum   = 1
  115.       end
  116.       else if index("+-/*", arg) ~= 0 then do
  117.          oper     = arg
  118.          register = number
  119.          newnum   = 1
  120.       end
  121.       else if arg = "=" then do
  122.          interpret "register"||arg||register||oper||number
  123.          number   = register
  124.          newnum   = 1
  125.       end
  126.       else nop
  127.  
  128. /*
  129. *   Update the dialogue area
  130. */
  131.       if quitflag ~= 1 then call WindowText(REXXCALCHOST, "\  "||number)
  132.  
  133.    end
  134. end
  135.  
  136. exit
  137.  
  138. /*
  139. *   Error trapping:
  140. */
  141. syntax:
  142. call WindowText(REXXCALCHOST, "\   Error")
  143. newnum = 1
  144. signal on syntax
  145. signal start
  146.